//
//  JQFXHMoliAdressyyPicker.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/12.
//

import Foundation
import UIKit

typealias JQFXHMoliAddressvvBlock = (String, Int)->Void


class JQFXHMoliAdressPickerViewController: UIViewController {
    var jqfxhSelectwwProvince = "北京"
    var jqfxhSelectyyArea = "东城区"
    var jqfxhMolizzCitys : [JQFXHCityffInfoData] = []
    var jqfxhAddresskkBlock : JQFXHMoliAddressvvBlock?
    var jqfxhSelectxxCity = "北京"

   
    private func jqfxhLoadAddressaaData() {
        
        let provider = JQFXHNetworkManager.shared.moliProvider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhCityssList, type: [JQFXHCityffInfoData].self) { result in
            switch result {
            case.success(let citys):
                self.jqfxhMolizzCitys = citys
                self.jqfxhContentbbView.addSubview(self.jqfxhPickerView)
                self.jqfxhPickerView.snp.makeConstraints { make in
                    make.left.right.equalTo(self.jqfxhContentbbView)
                    make.top.equalTo(self.jqfxhTitleccLabel.snp.bottom).offset(10)
                    make.bottom.equalTo(self.jqfxhQueeeButton.snp.top).offset(-10)
                }
            case .failure(let error):
                print(error)
            }
        }

    }
    
    func jqfxhongeong() -> String {
        let token = "0163f0e9439d463a14f388855388c49b"
        return token
    }
    
    @objc func jqfxhCancelBtnffClick() {
        self.dismiss(animated: true)
    }
    
    @objc func jqfxhQueBtnhhClick() {
        let selectedProvinces = jqfxhPickerView.selectedRow(inComponent: 0)
        self.jqfxhSelectwwProvince = self.jqfxhMolizzCitys[selectedProvinces].fullName
        
        let selectedCity = jqfxhPickerView.selectedRow(inComponent: 1)
        self.jqfxhSelectxxCity = self.jqfxhMolizzCitys[selectedProvinces].childrenKey[selectedCity].fullName
        
        let selectedArea = jqfxhPickerView.selectedRow(inComponent: 2)
        self.jqfxhSelectyyArea = self.jqfxhMolizzCitys[selectedProvinces].childrenKey[selectedCity].childrenKey[selectedArea].fullName
        let lastCode = self.jqfxhMolizzCitys[selectedProvinces].childrenKey[selectedCity].childrenKey[selectedArea].codeVaule


        self.jqfxhAddresskkBlock?(jqfxhSelectwwProvince+jqfxhSelectxxCity+jqfxhSelectyyArea, lastCode)
        self.dismiss(animated: true)

    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.jqfxhLoadAddressaaData()
        
        self.view.addSubview(jqfxhContentbbView)
        jqfxhContentbbView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view.snp.centerY)
        }
        jqfxhContentbbView.addSubview(jqfxhTitleccLabel)
        jqfxhTitleccLabel.snp.makeConstraints { make in
            make.centerX.equalTo(jqfxhContentbbView)
            make.top.equalTo(jqfxhContentbbView).offset(10)
            make.height.equalTo(32)
        }
        
        jqfxhContentbbView.addSubview(jqfxhCancleddBtn)
        jqfxhCancleddBtn.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(28)
            make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom-30)
            make.height.equalTo(37)
        }
        
        jqfxhContentbbView.addSubview(jqfxhQueeeButton)
        jqfxhQueeeButton.snp.makeConstraints { make in
            make.right.equalTo(self.view).offset(-28)
            make.left.equalTo(jqfxhCancleddBtn.snp.right).offset(30)
            make.width.equalTo(jqfxhCancleddBtn)
            make.bottom.height.equalTo(self.jqfxhCancleddBtn)
        }
        

    }
   
    
    
    lazy var jqfxhTitleccLabel : UILabel = {
        let navjqfxhTitleccLabel = UILabel(frame: CGRectMake(0, 0, jqfxhScreenWidth, 34))
        navjqfxhTitleccLabel.font = UIFont.jqfxhBold18
        navjqfxhTitleccLabel.textColor = UIColor.black
        navjqfxhTitleccLabel.text = "地址选择"
        return navjqfxhTitleccLabel
    }()
    
    
    lazy var jqfxhContentbbView : UIView = {
        let jqfxhContentbbView = UIView(frame: CGRect(x: 0, y: jqfxhScreenHeight/2, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2))
        jqfxhContentbbView.backgroundColor = UIColor.white
        return jqfxhContentbbView
    }()
    
   
    lazy var jqfxhCancleddBtn: UIButton = {
        let nextButton = UIButton(type: .system)
        nextButton.frame = CGRect(x: 30, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        nextButton.setTitle("取消", for: .normal)
        nextButton.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        nextButton.setTitleColor(.lightGray, for: .normal)
        nextButton.layer.cornerRadius = 8
        nextButton.addTarget(self, action: #selector(jqfxhCancelBtnffClick), for: .touchUpInside)
        return nextButton
    }()
    
    lazy var jqfxhQueeeButton: UIButton = {
        let nextButton = UIButton(type: .system)
        nextButton.frame = CGRect(x: jqfxhScreenWidth/2+15, y: jqfxhScreenHeight/2-UIApplication.shared.jqfxhSafeAreaInsets.bottom-35, width: (jqfxhScreenWidth-90)/2, height: 35)
        nextButton.setTitle("确定", for: .normal)
        nextButton.backgroundColor = UIColor.jqfxhThemexxRed
        nextButton.setTitleColor(.white, for: .normal)
        nextButton.layer.cornerRadius = 8
        nextButton.addTarget(self, action: #selector(jqfxhQueBtnhhClick), for: .touchUpInside)
        return nextButton
    }()
    
    lazy var jqfxhPickerView : UIPickerView = {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: 35, width: jqfxhScreenWidth, height: jqfxhScreenHeight/2-35-UIApplication.shared.jqfxhSafeAreaInsets.bottom-40))
        pickerView.backgroundColor = UIColor.white
        pickerView.dataSource = self
        pickerView.delegate = self
        return pickerView
    }()
    
    
}


extension JQFXHMoliAdressPickerViewController : UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return jqfxhMolizzCitys.count
        } else if component == 1{
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhMolizzCitys[selectedProvinceIndex].childrenKey.count
        }else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            let citys = jqfxhMolizzCitys[selectedProvinceIndex].childrenKey
            let selectedCityIndex = pickerView.selectedRow(inComponent: 1)
            return citys[selectedCityIndex].childrenKey.count


        }
    }
}

extension JQFXHMoliAdressPickerViewController : UIPickerViewDataSource {
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            return jqfxhMolizzCitys[row].fullName
        } else if component == 1{
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            return jqfxhMolizzCitys[selectedProvinceIndex].childrenKey[row].fullName
        }else {
            let selectedProvinceIndex = pickerView.selectedRow(inComponent: 0)
            let citys = jqfxhMolizzCitys[selectedProvinceIndex].childrenKey
            let selectedCityIndex = pickerView.selectedRow(inComponent: 1)
            return citys[selectedCityIndex].childrenKey[row].fullName
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            pickerView.reloadComponent(1)
            pickerView.selectRow(0, inComponent: 1, animated: true)
            pickerView.reloadComponent(2)
            pickerView.selectRow(0, inComponent: 2, animated: true)
        }else if component == 1 {
            pickerView.reloadComponent(2)
            pickerView.selectRow(0, inComponent: 2, animated: true)
           
        }else {
            
        }
    }
}




